package interceptcrossing.ui;

import interceptcrossing.Intercept;
import interceptcrossing.InterceptGenerator;
import interceptcrossing.ui.command.GenerateInterceptsCommand;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Set;

public class GenerationDialog extends JDialog {

	private MainWindowDirector windowDirector;

	private JButton generateButton;
	private JButton cancelButton;
	private JSpinner minXSpinner;
	private JSpinner maxXSpinner;
	private JSpinner minYSpinner;
	private JSpinner maxYSpinner;
	private JSpinner interceptCountSpinner;
	private JCheckBox gaussianCheckBox;

	public GenerationDialog(Frame owner, MainWindowDirector windowDirector) {
		super(owner, "Генерирование отрезков", true);

		this.windowDirector = windowDirector;

		double min = -1e+5;
		double max = 1e+5;

		this.generateButton = new JButton("Сгенерировать");
		this.cancelButton = new JButton("Отмена");
		this.minXSpinner = new JSpinner(new SpinnerNumberModel(-100.0, min, max, 1.0));
		this.maxXSpinner = new JSpinner(new SpinnerNumberModel(100.0, min, max, 1.0));
		this.minYSpinner = new JSpinner(new SpinnerNumberModel(-100.0, min, max, 1.0));
		this.maxYSpinner = new JSpinner(new SpinnerNumberModel(100.0, min, max, 1.0));
		this.interceptCountSpinner = new JSpinner(new SpinnerNumberModel(10, 0, 100, 1));
		this.gaussianCheckBox = new JCheckBox("Нормальное распределение", true);

		this.initLayout();
		this.initActions();

		super.pack();
		super.setLocationRelativeTo(owner);
	}

	private void initLayout() {
		super.setLayout(new GridBagLayout());

		JPanel optionsPanel = new JPanel();
		optionsPanel.setLayout(new GridLayout(5, 2, 10, 10));
		optionsPanel.add(new JLabel("Количество:"));
		optionsPanel.add(this.interceptCountSpinner);
		optionsPanel.add(new JLabel("<html>X<sub>min</sub>:</html>"));
		optionsPanel.add(this.minXSpinner);
		optionsPanel.add(new JLabel("<html>Y<sub>min</sub>:</html>"));
		optionsPanel.add(this.minYSpinner);
		optionsPanel.add(new JLabel("<html>X<sub>max</sub>:</html>"));
		optionsPanel.add(this.maxXSpinner);
		optionsPanel.add(new JLabel("<html>Y<sub>max</sub>:</html>"));
		optionsPanel.add(this.maxYSpinner);

		super.add(optionsPanel, new GridBagConstraints(1, 1, 2, 1, 1.0, 0.5,
			GridBagConstraints.CENTER, GridBagConstraints.BOTH,
			new Insets(5, 5, 5, 5), 0, 0));

		super.add(this.gaussianCheckBox, new GridBagConstraints(1, 2, 2, 1, 1.0, 0.0,
			GridBagConstraints.WEST, GridBagConstraints.NONE,
			new Insets(5, 5, 5, 5), 0, 0));

		super.add(this.generateButton, new GridBagConstraints(1, 3, 1, 1, 0.5, 0.0,
			GridBagConstraints.CENTER, GridBagConstraints.NONE,
			new Insets(5, 5, 5, 5), 0, 0));

		super.add(this.cancelButton, new GridBagConstraints(2, 3, 1, 1, 0.5, 0.0,
			GridBagConstraints.CENTER, GridBagConstraints.NONE,
			new Insets(5, 5, 5, 5), 0, 0));
	}

	private void initActions() {
		this.cancelButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				GenerationDialog.this.setVisible(false);
			}
		});

		this.generateButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				InterceptGenerator generator = new InterceptGenerator(
					(Double) minXSpinner.getValue(),
					(Double) maxXSpinner.getValue(),
					(Double) minYSpinner.getValue(),
					(Double) maxYSpinner.getValue());

				Set<Intercept> intercepts = generator.generate(
					(Integer) interceptCountSpinner.getValue());

				windowDirector.getCommandManager().doCommand(
					new GenerateInterceptsCommand(windowDirector, intercepts));
				GenerationDialog.this.setVisible(false);
			}
		});
	}
}
